package com.org.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.org.bean.setting.RegionInfo;
import com.org.service.RegionService;
@Controller
@RequestMapping("/region")
public class RegionAction {
	@Autowired
	private RegionService regionService;
	private Iterator<String> iter;
	private List<String> list;

	private Map<String, Object> status(List<RegionInfo> regionInfo) {
		// TODO Auto-generated method stub
		Map<String, Object> m = new HashMap<String, Object>();
		if (regionInfo.size() != 0) {
			m.put("statusCode", 1);
			m.put("msg", "成功");
			m.put("result", regionInfo);
		} else {
			m.put("statusCode", 0);
			m.put("msg", "失败");

		}
		return m;
	}

	private Map<String, Object> status(Boolean regionInfo) {
		// TODO Auto-generated method stub
		Map<String, Object> m = new HashMap<String, Object>();
		if (regionInfo) {
			m.put("statusCode", 1);
			m.put("msg", "成功");
		} else {
			m.put("statusCode", 0);
			m.put("msg", "失败");

		}
		return m;
	}

	@RequestMapping("/findAll")
	public @ResponseBody Map<String, Object> getRegionInfos() {
		List<RegionInfo> regionInfo = new ArrayList<RegionInfo>();
		regionInfo = regionService.findAll();
		return status(regionInfo);
	}

	@RequestMapping("/{regionId}/findById")
	public @ResponseBody Map<String, Object> getRegionInfo(
			@PathVariable int regionId) {
		List<RegionInfo> regionInfo = new ArrayList<RegionInfo>();
		regionInfo = regionService.findById(regionId);
		return status(regionInfo);
	}
	
	@RequestMapping("/{regionName}/findByName")
	public @ResponseBody Map<String, Object> getRegionId(
			@PathVariable String regionName) {
		List<RegionInfo> regionInfo = new ArrayList<RegionInfo>();
		regionInfo = regionService.findByName(regionName);
		return status(regionInfo);
	}

	@RequestMapping("/addRegion")
	public @ResponseBody Map<String, Object> addRegion(
			@RequestBody Map<String, Object> regionMap) {
		boolean result;
		iter = regionMap.keySet().iterator();
		list = new ArrayList<String>();
		while (iter.hasNext()) {
			list.add(iter.next());
		}
		RegionInfo regionInfo = new RegionInfo();
		regionInfo.setRegionName((String) regionMap.get(list.get(0)));
		result = regionService.addRegion(regionInfo);
		return status(result);
	}

	@RequestMapping("/updateRegion")
	public @ResponseBody Map<String, Object> updateRegion(
			@RequestBody Map<String, Object> regionMap) {
		boolean result;
		iter = regionMap.keySet().iterator(); // 获取Map集合的key值
		list = new ArrayList<String>();
		while (iter.hasNext()) {
			list.add(iter.next());
		}
		RegionInfo regionInfo = new RegionInfo();
		regionInfo.setRegionName((String) regionMap.get(list.get(0)));
		result = regionService.addRegion(regionInfo);
		return status(result);
	}

	@RequestMapping("/{regionId}/deleteRegion")
	public @ResponseBody Map<String, Object> deleteUser(@PathVariable int regionId) {
		boolean result;
		result = regionService.deleteRegion(regionId);
		return status(result);
	}

}
